// pages/creditCardCreate/index.js
import md5 from "../../utils/md5.js"
const net = require('../../utils/net.js')
import { inputgetName } from "../../utils/inputBothBind.js"
const app = getApp()
Page({
  data: {
    stepText: ["1.身份认证", "2.储蓄卡认证"],
    cardType: {
      name: '',
      id: 10
    },
    cardNum: '666',
    validDt: '777',
    bank:"",
    staticImgUrl: app.globalData.staticImgUrl
  },
  toSelectCardType: function () {
    console.log("66666")
    wx.navigateTo({
      // TODO
      url: ""
    })
  },
  toBankCardOCR: function () {
    console.log("bankcard OCR")
    // TODO
  },
  toSelectValidDt: function () {
    // TODO
    console.log("选择有效期")
  },
  toNext: function () {
    // TODO
    if (!this.data.cardNum || !this.data.validDt || !this.data.cardType.id) {
      wx.showToast({
        title: '请填写完整',
        icon: 'none'
      })
      return
    }

  },
  upShopLogo: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        console.log(res);
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImageShop('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImageShop('camera')
          }
        }
      }
    })
  },
  chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        var tempFilesSize = res.tempFiles[0].size;
        if (tempFilesSize <= 4190000) {   //图片小于或者等于2M时 可以执行获取图片
          console.log(res);
          that.upload_file(res.tempFilePaths[0])
        } else {    //图片大于2M，弹出一个提示框
          wx.showToast({
            title: '上传图片不能大于4M!',  //标题
            icon: 'none'       //图标 none不使用图标，详情看官方文档
          })
        }


        /*上传多张（遍历数组，一次传一张）
           for (var index in res.tempFilePaths) {
              that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index])
           }
       */
      }
    })
  },
  upload_file: function (filePath) {
    var that = this;
    var data = {};
    let params = md5(data);
    let token = wx.getStorageSync("xlk_token")
    let uid = wx.getStorageSync("xlk_uid")
    console.log(params);
    wx.uploadFile({
      url: 'http://www.a.com/api/index/index?method=Bank_card',
      filePath: filePath,
      name: 'file',
      method: 'POST',
      header: {
        "token": token,
        "uid": uid,
        'edition': '1.3.4',
        "model": "web",
        "platform": "xinlianka"
      }, // 设置请求的 header
      formData: { ...params }, // HTTP 请求中其他额外的 form data
      success: function (res) {
        console.log(res.data);
        if (res.data == 1) {
          wx.showToast({
            title: '上传图片未识别到信息，请重试',  //标题
            icon: 'none',       //图标 none不使用图标，详情看官方文档
            duration: 3000
          })
        } else {
          var a = res.data.split(',');
          console.log(a);
          var s1 = a['0'].replace('"', '');
          console.log(b1);
          var sd = a['1'];
          var b2 = sd.replace('"', '');
          var b1 = unescape(b2.replace(/\\u/g, "%u"));

          that.setData({
            bank: b1,
            cardNum: s1
          })
          wx.showToast({
            title: "图片识别成功",
            icon: 'success',
            duration: 700
          })
        }

      },
      fail: function (res) {

      }
    })
  },
  onLoad: function (options) {
    console.log(options)
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})